<template>
	<view class="">
		<u-loading-page :loading="isLoading"></u-loading-page>

		<view class="app" v-show="!isLoading">
			<u-gap height="20rpx"></u-gap>
			<view class="content-box u-m-t-0 u-p-40">{{ (obj.status == '待发货') & (obj.order_type == 2) ? '待核销' : obj.status }}</view>
			<!-- 商品 -->
			<view class=" content-box">
				<block v-for="(item, i) in obj.orderCommoditys" :key="i">
					<view class=" u-p-20 u-flex">
						<u-image width="160rpx" height="160rpx" :src="item.master"></u-image>
						<view class="u-flex-1  u-m-l-20 u-flex-between">
							<!-- <text class="u-line-2">{{ item.name }}</text> -->
							<u-text :text="item.name" lines="2"></u-text>
							<u-text :text="item.pvs_value_new"></u-text>
						</view>
						<view class="price-div u-flex-column">
							<u-text mode="price" :text="item.sell_price"></u-text>
							<u-text :text="'x' + item.count"></u-text>
						</view>
					</view>
					<u-cell-group :border="false">
						<u-cell :border="false" v-for="(attach, index) in item.attachlist" :title="attach.attach_value">
							<view class="u-flex" slot="right-icon">
								<u-text mode="price" :text="attach.attach_price"></u-text>
								<u-text :text="'x' + attach.num"></u-text>
							</view>
						</u-cell>
					</u-cell-group>
					<u-line></u-line>
				</block>
				

				<u-cell-group :border="false">
					<u-cell title="商品小计">
						<view class="" slot="right-icon">
							<u-text mode="price" :text="orderItemCountPrice"></u-text>
						</view>
					</u-cell>
					<u-cell title="运费">
						<view class="" slot="right-icon">
							<u-text mode="price" :text="obj.freight"></u-text>
						</view>
					</u-cell>
					<u-cell title="优惠抵扣">
						<view class="" slot="right-icon">
							<u-text color="#f56c6c" mode="price" :text="obj.discount"></u-text>
						</view>
					</u-cell>
					<u-cell title="积分抵扣" >
						<view class="" slot="right-icon">
							<u-text color="#f56c6c" mode="price" :text="obj.discount_amount"></u-text>
						</view>
					</u-cell>
					<u-cell title="会员折扣">
						<view class="" slot="right-icon">
							<u-text color="#f56c6c" mode="price" :text="+obj.discount_level_money"></u-text>
						</view>
					</u-cell>
					<u-cell title="实付款">
						<view class="" slot="right-icon">
							<u-text bold color="#f56c6c" mode="price" :text="obj.allprice"></u-text>
						</view>
					</u-cell>
				</u-cell-group>
			</view>
			<!-- 订单信息 -->
			<view class="content-box">
				<u-cell-group>
					<u-cell title="订单编号" :value="obj.order_no"></u-cell>
					<u-cell title="支付流水号" :value="obj.pay_no"></u-cell>
					<u-cell title="内部单号" :value="obj.unified_order_no"></u-cell>
					<u-cell title="付款方式" :value="obj.pay_id"></u-cell>
					<u-cell v-if="obj.user" title="买家" :value="obj.user.nickname"></u-cell>
					<u-cell title="配送方式">
						<view class="" slot="right-icon">
							<text class="txt" v-if="obj.order_type == 1">快递</text>
							<text class="txt" v-else-if="obj.order_type == 2">自提</text>
							<text class="txt" v-else-if="obj.order_type == 3">同城</text>
						</view>
					</u-cell>

					<u-cell v-if="obj.order_type != 1" :title="obj.order_type == 2 ? '自提时间:' : '配送时间:'" :value="obj.delivery_time"></u-cell>
					<u-cell title="来源" :value="obj.source">
						<view class="" slot="right-icon">
							<text class="txt" v-if="obj.source == 1">微信小程序</text>
							<text class="txt" v-else-if="obj.source == 2">微信公众号</text>
							<text class="txt" v-else-if="obj.source == 3">H5</text>
						</view>
					</u-cell>
					<u-cell title="收货人" :value="obj.consignee"></u-cell>
					<u-cell title="联系号码" :value="obj.iphone"></u-cell>
					<u-cell title="收货地址" :value="obj.address"></u-cell>
					<u-cell title="买家备注" :value="obj.remake"></u-cell>
				</u-cell-group>
			</view>
			<u-gap height="120rpx"></u-gap>
			<!-- 按钮组 -->
			<view class="footer u-flex u-flex-items-center">
				<!-- <u-button customStyle="margin:0 10rpx" size="small" type="primary" v-if="obj.status == '待支付' && login_type_url != 'shop'" @click="changeState(obj.id)">
				确认付款
			</u-button> -->
				<u-button v-has="{id:8}" customStyle="margin:0 10rpx" size="small" type="primary" v-if="obj.status == '待支付'" @click="editMoneyShow(obj.id)">改价</u-button>
				<u-button v-has="{id:8}" customStyle="margin:0 10rpx" size="small" type="primary" v-if="obj.status == '待支付'" @click="showmodel(1, obj.id)">关闭订单</u-button>
				<u-button v-has="{id:8,type:'refund'}" customStyle="margin:0 10rpx" size="small" type="primary" v-if="obj.status == '待发货' || obj.status == '待核销'" @click="showmodel(2, obj.id)">
					退款
				</u-button>
				<u-button
					customStyle="margin:0 10rpx"
					size="small"
					type="primary"
					v-if="(obj.status == '待发货' || obj.status == '部分发货') && obj.order_type == 1"
					@click="toExpress(obj)"
					v-has="{id:8,type:'deliver'}"
				>
					确认发货
				</u-button>
				<u-button
					customStyle="margin:0 10rpx"
					size="small"
					type="primary"
					v-if="(obj.status == '待发货' || obj.status == '部分发货') && obj.order_type == 3"
					@click="toExpress(obj, 1)"
					v-has="{id:8,type:'deliver'}"
				>
					确认发货
				</u-button>
				<u-button
					customStyle="margin:0 10rpx"
					size="small"
					type="primary"
					v-if="(obj.status == '待支付' || obj.status == '待发货') && obj.order_type == 1"
					@click="changeAddress(obj)"
					v-has="{id:8,type:'deliver'}"
				>
					修改收货信息
				</u-button>
				<u-button
					customStyle="margin:0 10rpx"
					size="small"
					type="primary"
					v-if="obj.orderCommoditys && obj.orderCommoditys.findIndex(_ => _.status == '已发货') > -1 && obj.order_type == 1 && login_type_url != 'shop'"
					@click="cancalExpress(obj, 0)"
					v-has="{id:8,type:'deliver'}"
				>
					取消发货
				</u-button>
				<u-button
					customStyle="margin:0 10rpx"
					size="small"
					type="primary"
					v-if="obj.orderCommoditys && obj.orderCommoditys.findIndex(_ => _.status == '已发货') > -1 && obj.order_type == 3 && login_type_url != 'shop'"
					@click="cancalExpress(obj, 1)"
					v-has="{id:8,type:'deliver'}"
				>
					取消发货
				</u-button>
			</view>
			<u-popup :show="showExpress" @close="showExpress = false"><orderExpress @close="close" :order="orderExpObj" @success="closeExpress"></orderExpress></u-popup>
			<u-modal :show="show_model" @cancel="show_model = false" :showCancelButton="true" @confirm="changestatus" ref="uModal" :asyncClose="true" :content="content"></u-modal>
			<u-popup :show="showAddress" @close="showAddress = false"><orderAddress @close="close" :order="orderAddressObj" @success="closeAddress"></orderAddress></u-popup>
			<u-popup :show="showCancel" @close="showCancel = false">
				<orderExpressCancel @close="close" ref="cancelComponent" @success="cancelSuccess"></orderExpressCancel>
			</u-popup>
			<u-popup :show="showMoneyEdit" @close="showMoneyEdit = false">
				<changeMoney ref="orderMoneyEditComponent" @close="close" @success="editMoneyClose"></changeMoney>
			</u-popup>
			<u-popup :show="showLocal" @close="showLocal = false"><orderLocal :order="orderExpObj" @success="closeLocal" @close="close"></orderLocal></u-popup>
			<u-popup :show="showCancellocal" @close="showCancellocal = false"><orderLocalCancal ref="LocalcancelComponent" @success="cancelSuccess" @close="close"></orderLocalCancal></u-popup>
		</view>
		<lc-fab-touch ref="fab" horizontal="right" vertical="bottom" direction="horizontal" />
	</view>
</template>

<script>
import {mapState, mapMutations} from 'vuex';
import orderExpress from './components/orderExpress';
import orderAddress from './components/orderAddress';
import orderExpressCancel from './components/orderExpressCancel';
import changeMoney from './components/changeMoney';
import orderLocal from './components/orderLocal';
import orderLocalCancal from './components/orderLocalCancal';

export default {
	data() {
		return {
			showMoneyEdit: false,
			rk: 1,
			showExpress: false,
			orderExpObj: {},
			showAddress: false,
			showLocal: false,
			showCancel: false,
			showCancellocal: false,
			dk: 1,
			orderAddressObj: {},
			isLoading: true,
			active: 1,
			orderItemCountPrice: '0.00',
			obj: {
				user: {}
			},
			showPrintReceipt: false,
			templateRecList: [],
			templateList: [],
			psoRec: {
				order_id: [],
				printer_id: '',
				template_id: ''
			},
			jur_addons: {},
			login_type_url: '',
			activeName: 'commodity',
			formlist: [],
			show_model: false,
			content: '',
			btn_type: 0
		};
	},
	computed: {
		...mapState({
			$BasicsUrl: state => state.$BasicsUrl
		})
	},
	components: {
		orderExpress,
		orderAddress,
		orderExpressCancel,
		changeMoney,
		orderLocal,
		orderLocalCancal
	},
	onLoad(e) {
		this.id = e.id;
		this.getdetail();
	},
	methods: {
		getdetail() {
			this.isLoading = true;
			uni.$u.http
				.get('/shop/order/' + this.id, {custom: {id: 8}})
				.then(res => {
					let o = res.msg;
					let x = 0;
					if (o.order_type == 3) {
						o.money = Number(o.money) - Number(o.distribution_fee);
						o.freight = o.distribution_fee.toFixed(2);
					}
					o.orderCommoditys.forEach(e => {
						if (e.master.indexOf('http') == -1) {
							e.master = this.$BasicsUrl + '/' + e.master;
						}
						//转换处理SKU数据->红+L+大
						if (e.pvs_value) {
							let l = e.pvs_value.split(','),
								s = '';
							l.forEach(k => {
								s += k.split(':')[1] + '+';
							});
							s = s.substring(0, s.length - 1);
							e.pvs_value_new = s;
						}
						//处理小计数据
						e.countPrice = (e.sell_price * e.count).toFixed(2);
						x += e.sell_price * e.count;
						if (o.order_type == 2 && e.status == '待发货') {
							e.status = '待核销';
						}
						if (o.attach && o.attach.length > 0 && e.attach_value && e.attach_value != '') {
							e.attachlist = [];
							e.attach_pirce = 0;

							o.attach.forEach(a => {
								if (e.attach_value.indexOf(a.attach_value) > -1 && e.attachlist.findIndex(_ => _.attach_value == a.attach_value) == -1) {
									if (e.sku_id) {
										if (e.sku_id == a.sku_id) {
											e.attachlist.push(a);
											e.attach_pirce += Number(a.attach_price) * a.num;
										}
									} else {
										e.attachlist.push(a);
										e.attach_pirce += Number(a.attach_price) * a.num;
									}
								}
							});

							x += e.attach_pirce;
							e.attach_pirce = e.attach_pirce.toFixed(2);
							e.countPrice = (Number(e.attach_pirce) + Number(e.countPrice)).toFixed(2);
						}
					});

					this.orderItemCountPrice = x.toFixed(2);
					let allprice = Number(o.money) - Number(o.discount) - Number(o.discount_amount) + Number(o.freight);
					o.allprice = allprice.toFixed(2);
					//this.obj = res.msg;
					this.obj = o;
					this.isLoading = false;
				})
				.catch(err => {});
		},
		//关闭订单 退款 二次确认
		showmodel(type, id) {
			this.btn_type = type;
			this.order_id = id;
			switch (type) {
				case 1:
					this.content = '是否关闭订单？';
					this.show_model = true;
					break;
				case 2:
					this.content = '是否退款？';
					this.show_model = true;
					break;
			}
		},
		changestatus() {
			switch (this.btn_type) {
				case 1:
					this.orderClose();
					break;
				case 2:
					this.orderdrawback();
			}
		},
		orderClose() {
			uni.$u.http
				.put('/shop/order/orderClose', {id: this.order_id}, {custom: {id: 8}})
				.then(res => {
					this.show_model = false;
					this.getdetail();
				})
				.catch(err => {});
		},
		orderdrawback() {
			uni.$u.http
				.post('/shop/order/drawback', {oid: this.order_id}, {custom: {id: 8}})
				.then(res => {
					this.show_model = false;
					this.getdetail();
				})
				.catch(err => {});
		},

		//确认发货
		toExpress(item, type) {
			this.orderExpObj = {...this.obj};
			//补全不同字段
			this.orderExpObj.user_real_name = this.obj.consignee;
			this.orderExpObj.user_mobile = this.obj.iphone;

			if (type) {
				this.showLocal = true;
				this.dk++;
			} else {
				this.rk++;

				this.$set(this.orderExpObj, 'cids', []);
				this.$set(this.orderExpObj, 'logistics_no', '');
				this.$set(this.orderExpObj, 'express_company', '');

				this.showExpress = true;
			}
		},
		//修改收货信息
		changeAddress(item) {
			//补全不同字段
			this.orderAddressObj = {
				id: this.obj.id,
				address: this.obj.address,
				user_real_name: this.obj.consignee,
				user_mobile: this.obj.iphone,
				status: this.obj.status
			};
			this.showAddress = true;
		},
		//取消发货
		cancalExpress(data, type) {
			let id = data.id;

			if (type == 1 && data.dada && data.dada.length > 0) {
				this.showCancellocal = true;
				this.$nextTick(_ => {
					this.$refs.LocalcancelComponent.init(id);
				});
			} else {
				this.showCancel = true;
				this.$nextTick(_ => {
					this.$refs.cancelComponent.init(id);
				});
			}
		},
		//显示变更余额
		editMoneyShow(id) {
			this.showMoneyEdit = true;
			this.$nextTick(_ => {
				this.$refs.orderMoneyEditComponent.init(id);
			});
		},

		/*组件回调函数*/
		//发货回调函数
		closeExpress() {
			this.showExpress = false;
			this.getdetail();
			// this.getlist();
		},

		//修改收货信息回调
		closeAddress() {
			this.showAddress = false;
			this.getdetail();
		},

		//取消发货回调
		cancelSuccess() {
			this.showCancel = false;
			this.showCancellocal = false;
			this.getdetail();
		},
		//变更余额回调
		editMoneyClose() {
			//重新加载数据
			this.showMoneyEdit = false;
			this.getdetail();
		},
		close(type) {
			this[type] = false;
		}
	}
};
</script>

<style lang="scss">
.content-box {
	margin: 20rpx auto;
}
.footer {
	height: 120rpx;
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	box-sizing: border-box;
}
.price-div {
	width: 160rpx;
}
</style>
